
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html><head>
  <title>form

 (web.py)</title>
  <link rel="stylesheet" type="text/css" href="http://webpy.org/static/webpy-new.css"/>
  <link rel="SHORTCUT ICON" href="http://webpy.org/static/favicon.ico"/>



</head><body>
<h1>form

</h1>


<div id="content">





<p>

<h2>Example</h2>
<p>Here's a sample script using the new form library:
</p>
<pre><code>import web
from web import form

render = web.template.render('templates/')

urls = ('/', 'index')

myform = form.Form( 
    form.Textbox(&quot;boe&quot;), 
    form.Textbox(&quot;bax&quot;, 
        form.notnull,
        form.regexp('\d+', 'Must be a digit'),
        form.Validator('Must be more than 5', lambda x:int(x)&gt;5)),
    form.Textarea('moe'),
    form.Checkbox('curly'), 
    form.Dropdown('french', ['mustard', 'fries', 'wine'])) 

class index: 
    def GET(self): 
        form = myform()
        ### make sure you create a copy of the form by calling it (line above)--otherwise changes will appear globally
        print render.formtest(form)

    def POST(self): 
        form = myform() 
        if not form.validates(): 
            print render.formtest(form)
        else:
            # form.d.boe and form['boe'].value are equivalent ways of
            # extracting the validated arguments from the form.
            print &quot;Grrreat success! boe: %s, bax: %s&quot; % (form.d.boe, form['bax'].value)

if __name__==&quot;__main__&quot;:
    web.internalerror = web.debugerror
    web.run(urls, globals(), web.reloader)
</code></pre><p>And sample formtest.html (place this in the <em>templates</em> subdirectory): 
</p>
<pre><code>$def with (form)

&lt;form name=&quot;main&quot; method=&quot;post&quot;&gt; 
$if not form.valid: &lt;p class=&quot;error&quot;&gt;Try again, AmeriCAN:&lt;/p&gt;
$:form.render()
&lt;input type=&quot;submit&quot; /&gt;    &lt;/form&gt;
</code></pre>
<h2>Form Features</h2>
<p>In addition individual input validators, form.py supports entire form validation which allows comparisons of fields.  The validators get passed as a list as the variable 'validators'.  For example:
</p>
<pre><code>signup = form.Form(
    form.Textbox('username'),
    form.Password('password'),
    form.Password('password_again'),
    validators = [form.Validator(&quot;Passwords didn't match.&quot;, lambda i: i.password == i.password_again)]
)
</code></pre><p>Once the form data has been posted, it can easily be put into a database (if the database scheme has names consistent with your webpy form).  For example:
</p>
<pre><code>def POST(self):
    f = myform()
    if f.validates():
        web.insert('data_table', **f.d)
    #don't do web.insert('data_table', **web.input()) because malicious data could be submitted too
    else:
        render.foo(f)
</code></pre>
<h2>Input Features</h2>
<p>The form inputs support several additional attributes.  For example:
</p>
<pre><code>form.textbox(&quot;firstname&quot;,
    form.notnull, #put validators first followed by optional attributes
    class_=&quot;textEntry&quot;, #gives a class name to the text box -- note the underscore
    pre=&quot;pre&quot;, #directly before the text box
    post=&quot;post&quot;, #directly after the text box
    description=&quot;please enter your name&quot;, #describes field, defaults to form name (&quot;firstname&quot;)
    value=&quot;bob&quot;, #default value
    id=&quot;nameid&quot;, #specify the id
)
</code></pre><p>In addition to the attributes above, any html attributes can be entered in the same manner.  For example:
</p>
<pre><code>myform2 = form.Form(
    form.textbox('phonenumber',
        size=&quot;12&quot;,
        maxlength=&quot;12&quot;        )
)
</code></pre><p><strong>Dropdown</strong>
</p>
<p>Dropdown inputs allow a unique description and value for each item in the dropdown list.  To do this, create the dropdown list with tuples like this:
</p>
<pre><code>form.Dropdown('mydrop', [('value1', 'description1'), ('value2', 'description2')])
</code></pre>

</p>
</body>
</html>

